/*
 * View presenting the results the user got while doing a test.
 * Can reach the user-home and redo the last test from here.
 */

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ResultView.java
 *
 * Created on 2011-maj-17, 16:54:47
 */
package edu.chalmers.se.tda367.gui.view;

import edu.chalmers.se.tda367.controller.TestController;
import java.util.List;
import edu.chalmers.se.tda367.main.Pluggis;
import edu.chalmers.se.tda367.model.Test;
import edu.chalmers.se.tda367.question.IQuestion;
import java.awt.Color;
import javax.swing.JOptionPane;

/**
 *
 * @author bgrn
 */
public class ResultView extends javax.swing.JPanel {

    /** Creates new form ResultView */
    public ResultView() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        redoTestButton = new javax.swing.JButton();
        pluggisRuggis = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        headerLabel = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        testResultArea = new javax.swing.JTextArea();
        userImageButton = new javax.swing.JButton();
        passedOrNot = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(edu.chalmers.se.tda367.main.Main.class).getContext().getResourceMap(ResultView.class);
        setBackground(resourceMap.getColor("Form.background")); // NOI18N
        setMaximumSize(new java.awt.Dimension(820, 550));
        setMinimumSize(new java.awt.Dimension(820, 550));
        setName("Form"); // NOI18N
        setPreferredSize(new java.awt.Dimension(820, 550));

        redoTestButton.setFont(resourceMap.getFont("redoTestButton.font")); // NOI18N
        redoTestButton.setText(resourceMap.getString("redoTestButton.text")); // NOI18N
        redoTestButton.setName("redoTestButton"); // NOI18N
        redoTestButton.setOpaque(false);
        redoTestButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                redoTestButtonActionPerformed(evt);
            }
        });

        pluggisRuggis.setIcon(resourceMap.getIcon("pluggisRuggis.icon")); // NOI18N
        pluggisRuggis.setText(resourceMap.getString("pluggisRuggis.text")); // NOI18N
        pluggisRuggis.setName("pluggisRuggis"); // NOI18N

        jPanel1.setBackground(resourceMap.getColor("jPanel1.background")); // NOI18N
        jPanel1.setName("jPanel1"); // NOI18N

        headerLabel.setFont(resourceMap.getFont("headerLabel.font")); // NOI18N
        headerLabel.setText(resourceMap.getString("headerLabel.text")); // NOI18N
        headerLabel.setName("headerLabel"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        testResultArea.setBackground(resourceMap.getColor("testResultArea.background")); // NOI18N
        testResultArea.setColumns(20);
        testResultArea.setFont(resourceMap.getFont("testResultArea.font")); // NOI18N
        testResultArea.setRows(5);
        testResultArea.setName("testResultArea"); // NOI18N
        jScrollPane1.setViewportView(testResultArea);

        userImageButton.setMaximumSize(new java.awt.Dimension(109, 126));
        userImageButton.setMinimumSize(new java.awt.Dimension(109, 126));
        userImageButton.setName("userImageButton"); // NOI18N
        userImageButton.setOpaque(false);
        userImageButton.setPreferredSize(new java.awt.Dimension(109, 126));
        userImageButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                userImageButtonActionPerformed(evt);
            }
        });

        passedOrNot.setFont(resourceMap.getFont("passedOrNot.font")); // NOI18N
        passedOrNot.setText(resourceMap.getString("passedOrNot.text")); // NOI18N
        passedOrNot.setName("passedOrNot"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 454, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(4, 4, 4)
                                .addComponent(passedOrNot, javax.swing.GroupLayout.PREFERRED_SIZE, 262, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(headerLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 266, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 90, Short.MAX_VALUE)
                        .addComponent(userImageButton, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(13, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(userImageButton, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(17, 17, 17))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(13, 13, 13)
                        .addComponent(headerLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(passedOrNot, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 380, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(37, 37, 37))
        );

        jLabel1.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(pluggisRuggis, javax.swing.GroupLayout.PREFERRED_SIZE, 313, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(21, 21, 21)
                        .addComponent(redoTestButton))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(26, 26, 26)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 250, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(19, 19, 19)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(pluggisRuggis, javax.swing.GroupLayout.PREFERRED_SIZE, 325, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(31, 31, 31)
                        .addComponent(redoTestButton, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(27, 27, 27)))
                .addGap(85, 85, 85))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void redoTestButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_redoTestButtonActionPerformed
        TestController tc = new TestController();
        tc.redoTest();
}//GEN-LAST:event_redoTestButtonActionPerformed

    private void userImageButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_userImageButtonActionPerformed
        int messageType = JOptionPane.QUESTION_MESSAGE;
        String[] options = {"Ja", "Nej"};
        int code = JOptionPane.showOptionDialog(null,
                "Vill du gå till ditt ugglebo?",
                "", 0, messageType,
                null, options, "Nej");
        if (code == 0) {
            Pluggis g = Pluggis.getInstance();
            g.getMainView().removeAll();
            g.getMainView().add(new LoggedInView());
            g.getMainView().repaint();
            g.getMainView().validate();
        }
    }//GEN-LAST:event_userImageButtonActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel headerLabel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel passedOrNot;
    private javax.swing.JLabel pluggisRuggis;
    private javax.swing.JButton redoTestButton;
    private javax.swing.JTextArea testResultArea;
    private javax.swing.JButton userImageButton;
    // End of variables declaration//GEN-END:variables

    void setVariables(Test test) {
        userImageButton.setIcon(Pluggis.getInstance().getSelectedUser().getImage());
        List<IQuestion> questionList = test.getQuestions();
        double totCorrectAnswers = 0;
        String result = "";
        String resultText = "";
        String passText = "";
        for (int i = 0; i < 20; i++) {
            if (questionList.get(i).isAnswered() == false) {
                result = "Du svarade inte.";
            } else if (questionList.get(i).getAnswer() == questionList.get(i).getUserAnswer()) {
                result = "Rätt svar!";
                totCorrectAnswers = totCorrectAnswers + 1;
            } else {
                result = "Du svarade " + questionList.get(i).getUserAnswer() + ". Fel svar!";
            }
            int x = i + 1;
            resultText = resultText + "Fråga nummer " + x + ": " + questionList.get(i).getQuestion() + " = " + questionList.get(i).getAnswer() + ". " + result + "\n";

        }
        if ((totCorrectAnswers / questionList.size()) * 10 >= 7) {
            System.out.println("" + (totCorrectAnswers / questionList.size()));
            passText = "Du klarade provet! Bra jobbat!";
            passedOrNot.setForeground(Color.green);
            passedOrNot.setText("1337!");
            int level = Pluggis.getInstance().getSelectedUser().getLevel() + 1;
            Pluggis.getInstance().getSelectedUser().setLevel(level);
        } else {
            passedOrNot.setForeground(Color.red);
            passedOrNot.setText("EPIC FAIL");
            passText = "Du klarade inte provet, försök igen!";
        }
        testResultArea.setText(resultText + "\n" + "\n" + passText);
    }
}
